package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Supplier;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ListFeignClient listFeignClient;
    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public Map<String, Object> getItemBySkuId(Long skuId) {
        //创建对象
        Map<String, Object> result = new HashMap<>();
//        Supplier
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
                    SkuInfo skuInfo = productFeignClient.skuInfo(skuId);
                    result.put("SkuInfo", skuInfo);
                    return skuInfo;
                }
        );

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        }, executor);

        CompletableFuture<Void> spuSaleCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String valueJSon = JSON.toJSONString(skuValueIdsMap);
            result.put("valueJSon", valueJSon);
        }, executor);

        CompletableFuture<Void> incrHotScoreFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        });

        CompletableFuture<Void> skuPriceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            result.put("skuPrice", skuPrice);
        });
        //使用多任务组合
        CompletableFuture.allOf(skuInfoCompletableFuture, categoryViewCompletableFuture, incrHotScoreFuture, spuSaleCompletableFuture, skuPriceFuture).join();
        return result;


//        SkuInfo skuInfo = productFeignClient.skuInfo(skuId);
//        if (skuInfo != null) {
//            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());


//            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
//            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
//            String valueJSon = JSON.toJSONString(skuValueIdsMap);
//            result.put("valueJSon", valueJSon);
//            result.put("spuSaleAttrListCheckBySku", spuSaleAttrListCheckBySku);
//            result.put("categoryView", categoryView);
//        }
//        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
//        result.put("skuPrice", skuPrice);
//
//        return result;
    }
}
